package UI;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import DBTasks.Login;
import DBTasks.ShelterList;

@SuppressWarnings("serial")
public class LoginPanel extends JPanel implements ActionListener{

	private JTextField userInput;
	private JPasswordField passwordInput;
	private JButton employeeLoginButton;
	private JButton guestLoginButton;
	private JComboBox shelterList;
	private MainFrame frame;

	public LoginPanel(MainFrame mainFrame) {
		
		this.frame = mainFrame;
		
		JLabel guest = new JLabel("Guest Login");
		guest.setFont(new Font("Tahoma", Font.CENTER_BASELINE, 20));
		JLabel employee = new JLabel("Employee Login");
		employee.setFont(new Font("Tahoma", Font.CENTER_BASELINE, 20));
		JLabel shelterName = new JLabel("Shelter Name:");
		JLabel username = new JLabel("Employee ID:");
		JLabel password = new JLabel("Password:");
		
		userInput = new JTextField(20);
		passwordInput = new JPasswordField(20);
		
		employeeLoginButton = new JButton("Login as Employee");
		employeeLoginButton.addActionListener(this);
		guestLoginButton = new JButton("Login as Guest");
		guestLoginButton.addActionListener(this);
		
		ArrayList<String> shelters = ShelterList.listShelters();
		shelterList = new JComboBox(shelters.toArray());
		shelterList.setSelectedIndex(0);
		shelterList.addActionListener(this);
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(guest)
				.addComponent(guestLoginButton)
				.addComponent(employee)
				.addGroup(gl.createSequentialGroup()
						.addGap(100)
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(shelterName)
								.addComponent(username)
								.addComponent(password))
						.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
								.addComponent(shelterList)
								.addComponent(userInput)
								.addComponent(passwordInput))
						.addGap(100)
								)
				.addComponent(employeeLoginButton));
		
		gl.linkSize(SwingConstants.VERTICAL, shelterList, userInput, passwordInput);
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(guest)
			    .addGap(15)
			    .addComponent(guestLoginButton)
			    .addGap(30)
			    .addComponent(employee)
			    .addGap(15)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.LEADING)
			        .addGroup(gl.createSequentialGroup()
			        	.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
					        .addComponent(shelterName)
					        .addComponent(shelterList))
					    .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(username)
			                .addComponent(userInput))
			            .addGap(20)
			            .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
			                .addComponent(password)
			                .addComponent(passwordInput)))
			     )
			     .addGap(15)
			     .addComponent(employeeLoginButton)
			     .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		gl.setAutoCreateGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }
	
	public String getUserName(){
		return userInput.getText();
	}
	
	@SuppressWarnings("deprecation")
	public String getPassword(){
		return passwordInput.getText();
	}
	
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) 
    {
            if(e.getSource().equals(guestLoginButton)) 
            {
            	frame.setPanel(frame.getSearch());
            }
            
            else if(e.getSource().equals(employeeLoginButton)) 
            {
            	try { 
            		if(Login.login(Integer.parseInt(userInput.getText()), (String) shelterList.getSelectedItem(), passwordInput.getText())) { 
            				frame.setPanel(frame.getHomepage());
            				frame.setEmployeePanels();
            		}
            		else {
            			System.out.println("INVALID ID");
            		}
          	    } 
          	    catch(Exception error) { 
          	    	error.printStackTrace();
          	    }
            }    
    }

	public JComboBox getShelterList() {
		return shelterList;
	}

	public void setShelterList(JComboBox shelterList) {
		this.shelterList = shelterList;
	}

}
